import { createStore, useStore as useVuexStore, Store } from 'vuex'
import { IRootState, IStoreType } from './type'

import { getPageListData } from '@/network/main/system/system'

import login from './login'
import system from './main/system/system'
import dashboard from './main/analysis/dashboard'

const store = createStore<IRootState>({
  state() {
    return {
      name: 'coderwhy',
      password: '123456',
      entireDepartment: [],
      entireRole: [],
      entireMenus: []
    }
  },
  mutations: {
    changeDepartmentList(state, payload: any) {
      state.entireDepartment = payload
    },
    changeRoleList(state, payload: any) {
      state.entireRole = payload
    },
    changeMenusList(state, payload: any) {
      state.entireMenus = payload
    }
  },
  actions: {
    async getInitialData({ commit }) {
      const departmentResult = await getPageListData(`/department/list`, {
        offset: 0,
        size: 100
      })
      const { list: departmentList } = departmentResult.data
      commit('changeDepartmentList', departmentList)

      const roleResult = await getPageListData(`/role/list`, {
        offset: 0,
        size: 100
      })
      const { list: roleList } = roleResult.data
      commit('changeRoleList', roleList)

      const menusResult = await getPageListData(`/menu/list`, {})
      const { list: menusList } = menusResult.data
      commit('changeMenusList', menusList)
    }
  },
  modules: {
    login,
    system,
    dashboard
  },
  getters: {}
})

export function setupStore() {
  store.dispatch('login/loadLocalLogin')
  // store.dispatch('getInitialData')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export default store
